package com.leetcode.LeetCode精选TOP面试题;

/**
 * @author 覃国强
 * @date 2022-05-22 17:23
 */
public class 题库_0038_外观数列 {

  static
  class Solution {
    public String countAndSay(int n) {
      if (n == 1) {
        return "1";
      }
      String answer = countAndSay(n - 1);
      StringBuilder sb = new StringBuilder();
      char candidate = answer.charAt(0);
      int count = 1;
      for (int i = 1; i < answer.length(); ++i) {
        char ch = answer.charAt(i);
        if (candidate != ch) {
          sb.append(count);
          sb.append(candidate);
          candidate = ch;
          count = 1;
        } else {
          ++count;
        }
      }
      sb.append(count);
      sb.append(candidate);
      return sb.toString();
    }
  }

}
